package com.horay.statemachiedemo.multi.controller;

import com.horay.statemachiedemo.multi.builder.ComplexStateMachineBuilder;
import com.horay.statemachiedemo.multi.builder.OrderStateMachineBuilder;
import com.horay.statemachiedemo.multi.enums.ComplexState;
import com.horay.statemachiedemo.multi.event.ComplexEvent;
import com.horay.statemachiedemo.multi.model.Form;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@RestController
public class ComplexController {

    private final BeanFactory beanFactory;

    private final ComplexStateMachineBuilder complexStateMachineBuilder;

    public ComplexController(BeanFactory beanFactory, OrderStateMachineBuilder orderStateMachineBuilder, ComplexStateMachineBuilder complexStateMachineBuilder) {
        this.beanFactory = beanFactory;
        this.complexStateMachineBuilder = complexStateMachineBuilder;
    }

    @PostMapping("/complex/state")
    public void testComplexFormState() throws Exception {

        StateMachine<ComplexState, ComplexEvent> stateMachine = complexStateMachineBuilder.build(beanFactory);
        log.info(stateMachine.getId());

        Form form1 = new Form();
        form1.setFormId("111");
        form1.setFormName(null);

        Form form2 = new Form();
        form2.setFormId("222");
        form2.setFormName("好的表单");

        Form form3 = new Form();
        form3.setFormId("333");
        form3.setFormName(null);

        // 创建流程
        log.info("-------------------form1------------------");

        stateMachine.start();
        Message message = MessageBuilder.withPayload(ComplexEvent.WRITE).setHeader(Form.class.getName(), form1).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.CHECK).setHeader(Form.class.getName(), form1).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.DEAL).setHeader(Form.class.getName(), form1).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.SUBMIT).setHeader(Form.class.getName(), form1).build();
        stateMachine.sendEvent(message);
        log.info("最终状态:" + stateMachine.getState().getId());

        log.info("-------------------form2------------------");
        stateMachine = complexStateMachineBuilder.build(beanFactory);
        stateMachine.start();
        message = MessageBuilder.withPayload(ComplexEvent.WRITE).setHeader(Form.class.getName(), form2).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.CHECK).setHeader(Form.class.getName(), form2).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.DEAL).setHeader(Form.class.getName(), form2).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.SUBMIT).setHeader(Form.class.getName(), form2).build();
        stateMachine.sendEvent(message);
        log.info("最终状态:" + stateMachine.getState().getId());

        log.info("-------------------form3------------------");
        stateMachine = complexStateMachineBuilder.build(beanFactory);
        stateMachine.start();
        message = MessageBuilder.withPayload(ComplexEvent.WRITE).setHeader(Form.class.getName(), form3).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.CHECK).setHeader(Form.class.getName(), form3).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        form3.setFormName("好的表单");
        message = MessageBuilder.withPayload(ComplexEvent.DEAL).setHeader(Form.class.getName(), form3).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.SUBMIT).setHeader(Form.class.getName(), form3).build();
        stateMachine.sendEvent(message);
        message = MessageBuilder.withPayload(ComplexEvent.CHECK).setHeader(Form.class.getName(), form3).build();
        stateMachine.sendEvent(message);
        log.info("当前状态:[{}]", stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexEvent.SUBMIT).setHeader(Form.class.getName(), form3).build();
        stateMachine.sendEvent(message);
        log.info("最终状态:" + stateMachine.getState().getId());
    }

}
